//
//  AppTools.m
//  UI12豆瓣大项目
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "AppTools.h"
#import "AFNetworking.h"
@implementation AppTools

+(void)getDataWithkey:(NSString *)name
                block:(AppToolsBlock)block
{
    //中文转码
    NSString *urlEncode = [name stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
    [manager GET:urlEncode parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        //block回调
        block(responseObject);
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
}

//这是系统的方法,不用了
+(void)getImage:(NSString *)imageUrl
          block:(AppToolsBlock)block{
    NSURL *url = [NSURL URLWithString:imageUrl];
    //创建请求对象
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    //创建网络连接对象
    NSURLSession *session = [NSURLSession sharedSession];
    //创建请求普通数据网络数据任务task
    NSURLSessionDataTask *task1 = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        //        UIImage *image = [UIImage imageWithData:data];
        block(data);
        
    }];
    [task1 resume];
    
}
@end
